package com.kylin.system.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kylin.common.core.exception.CheckException;
import com.kylin.common.core.service.impl.DataServiceImpl;
import com.kylin.system.entity.SysPost;
import com.kylin.system.mapper.SysPostMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 岗位信息 服务层处理
 *
 * @author wuhao
 * @since 1.0 - 2021-03-28
 */
@Service
public class SysPostService extends DataServiceImpl<SysPostMapper, SysPost> {

    @Override
    public int save(SysPost sysPost) {
        // 唯一性校验
        QueryWrapper<SysPost> where = new QueryWrapper<>();
        where.ne(ObjectUtils.isNotEmpty(sysPost.getId()),"id", sysPost.getId())
                .and(i ->
                        i.eq("post_code", sysPost.getPostCode())
                        .or().eq("post_name", sysPost.getPostName()));
        SysPost item = this.mapper.selectOne(where);
        if(item != null) {
            throw new CheckException("岗位名称或岗位编码已存在");
        }
        return super.save(sysPost);
    }

    /**
     * 根据用户ID获取岗位选择框列表
     *
     * @param userId 用户ID
     * @return 选中岗位ID列表
     */
    public List<Long> selectPostListByUserId(Long userId)
    {
        return this.mapper.selectPostListByUserId(userId);
    }
}
